<?php

namespace zhtfrontend\models\datas;

use common\models\datas\IsuserInfo;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%orders_commission}}".
 *
 * @property integer $id
 * @property integer $orders_id
 * @property integer $salesman_id
 * @property integer $status
 * @property integer $settlement_time
 * @property string $settlement_amount
 * @property string $commission_money
 * @property integer $orders_time
 * @property integer $created_at
 * @property integer $type
 * @property integer $proportion
 */
class OrdersCommission extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%orders_commission}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['orders_id', 'salesman_id', 'status', 'settlement_time', 'orders_time', 'created_at'], 'integer'],
            [['settlement_amount', 'commission_money'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'orders_id' => Yii::t('zhtfrontend', '订单id'),
            'salesman_id' => Yii::t('zhtfrontend', '发卡人id'),
            'status' => Yii::t('zhtfrontend', '结算状态（0 未结算 1 已结算）'),
            'settlement_time' => Yii::t('zhtfrontend', '结算时间'),
            'settlement_amount' => Yii::t('zhtfrontend', '需结算金额'),
            'commission_money' => Yii::t('zhtfrontend', '佣金'),
            'orders_time' => Yii::t('zhtfrontend', '订单时间'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'type' => Yii::t('zhtfrontend', '类型 0 发卡人 1 老师 2 班主任'),
            'proportion' => Yii::t('zhtfrontend', '佣金比例'),
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function(){
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['status'] = function (){
            switch($this->status){
                case 0:
                    return \Yii::t('zhtfrontend','未结算');
                case 1:
                    return \Yii::t('zhtfrontend','已结算');
            }
        };
        return $fields;
    }

    public function getOrders(){
        return $this->hasOne(Orders::className(),['id'=>'orders_id']);
    }

    public function getSalesman(){
        return $this->hasOne(IsuserInfo::className(),['id'=>'salesman_id']);
    }

    public function getTeacher(){
        return $this->hasOne(Teacher::className(),['id'=>'salesman_id']);
    }

        public function getHeadmaster(){
        return $this->hasOne(Headmaster::className(),['id'=>'salesman_id']);
    }


}
